//
//  RingView.m
//  GetFit3.0
//
//  Created by 秀才 on 2016/12/3.
//  Copyright © 2016年 lxc. All rights reserved.
//

#import "RingView.h"

@implementation RingView
{
    UILabel *_centerLabel;
    UILabel *_currentIdentityLabel;
    UILabel *_targetLabel;
}
- (instancetype)init
{
    self = [super init];
    if (self) {

    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _centerLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 40)];
        _centerLabel.center = CGPointMake(self.width/2, self.height/2);
        _centerLabel.font = [UIFont systemFontOfSize:12.0f];
        _centerLabel.textAlignment = NSTextAlignmentCenter;
        _centerLabel.textColor = [UIColor whiteColor];
        [self addSubview:_centerLabel];
        
        _targetLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, 80, 40)];
        _targetLabel.center = CGPointMake(self.width/2, self.height/2);
        _targetLabel.y = _targetLabel.y+30;
        _targetLabel.font = [UIFont systemFontOfSize:12.0f];
        _targetLabel.textAlignment = NSTextAlignmentCenter;
        _targetLabel.textColor = [UIColor whiteColor];

        [self addSubview:_targetLabel];
        
        _currentIdentityLabel = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, self.width, 40)];
        _currentIdentityLabel.center = CGPointMake(self.width/2, self.height/2-30);
        _currentIdentityLabel.textAlignment = NSTextAlignmentCenter;
        _currentIdentityLabel.font = [UIFont systemFontOfSize:12.0f];
        _currentIdentityLabel.textColor = [UIColor whiteColor];

        [self addSubview:_currentIdentityLabel];
    }
    return self;
}

- (void)initHomeChartProgress:(NSInteger)progress ringCenterLabel:(NSInteger)centerString ringTarget:(NSInteger)target currentIdentity:(NSInteger )currentIdentity
{

    _progressAngle = progress;
    _lineWidth = 15;

    if (currentIdentity ==0){
        _currentIdentity = HHH_Text(@"  Steps ");
        _centerString = [NSString stringWithFormat:@"%ld",(long)centerString];
        _targetLabel.text = [NSString stringWithFormat:@"%@ %ld",HHH_Text(@" Target  "),(long)target];
        
        _centerLabel.text = _centerString;
        _currentIdentityLabel.text = _currentIdentity;
    }
    else if(currentIdentity == 1){
        _currentIdentity = HHH_Text(@" Sleep  ");
        _centerString = centerString>60?[NSString stringWithFormat:@"%d%@%.2d%@", centerString / 60, HHH_Text(@" Hour  "),centerString % 60, HHH_Text(@" Min  ")]:[NSString stringWithFormat:@"%d%@",centerString%60,HHH_Text(@" Min  ")];
        _targetLabel.text = [NSString stringWithFormat:@"%@ %ld",HHH_Text(@" Target  "),(long)target];
        
        _centerLabel.text = _centerString;
        _currentIdentityLabel.text = _currentIdentity;

    }else if(currentIdentity == 3){
        _currentIdentity = @"";
        _lineWidth = 10;
        _progressAngle = 360*5;
    }
//    else if(currentIdentity == 99)
//    {
//      _centerString = [NSString stringWithFormat:@"%ld",centerString];
//    _centerLabel.text = _centerString;
//        
//    }
    
    //一些文字显示
//    _centerLabel.text = _centerString;
//    _currentIdentityLabel.text = _currentIdentity;
    
    if (self.tag == 100)
    {
        return;
    }
    
    
    
    
    [self setNeedsDisplay];
}

-(void)updateCurrentSteps:(NSString *)step
{
    _currentIdentity = @"步数";
    _centerString = step;
    _centerLabel.text = _centerString;
}

- (void)drawRect:(CGRect)rect
{
    if (self.tag == 100)
    {
        return;
    }

    CAShapeLayer *_bottomLayer;
    UIBezierPath *bottomPath;
    CAShapeLayer *_progressLayer;
    UIBezierPath *progressPath;
    _bottomLayer = [CAShapeLayer layer];
    _progressLayer = [CAShapeLayer layer];
    bottomPath = [UIBezierPath bezierPath];
    progressPath = [UIBezierPath bezierPath];
    
    [self.layer addSublayer:_bottomLayer];
    
    NSInteger _startAngle = -90;
    NSInteger _endAngle = 360;
    //填充颜色
    _bottomLayer.fillColor = nil;
    _bottomLayer.lineWidth = _lineWidth;
//    _bottomLayer.strokeColor = [UIColor lightGrayColor].CGColor;
    
    _bottomLayer.strokeColor = [UIColor lightGrayColor].CGColor;
    
    _bottomLayer.strokeStart = 0;

    [bottomPath addArcWithCenter:CGPointMake(self.width/2, self.height/2) radius:self.frame.size.height/3 startAngle:_startAngle/(180/M_PI)endAngle:(_startAngle+_endAngle)/(180/M_PI) clockwise:YES];
    _bottomLayer.path = bottomPath.CGPath;
    _bottomLayer.strokeEnd = 1;
    
    [self.layer addSublayer:_progressLayer];
    _progressLayer.fillColor = nil;
    _progressLayer.lineWidth = _lineWidth;
//    _progressLayer.strokeColor = [UIColor colorFromHex:0x03A9F4].CGColor;
//    _progressLayer.strokeColor = [UIColor colorFromHex:0x03A9F4].CGColor;
    _progressLayer.strokeColor = BFONTLIGHTCOLOR.CGColor;
    _progressLayer.strokeStart = 0;
    
    //圆环宽度和虚线间距

//    if (_lineWidth == 10) {
        _bottomLayer.lineDashPattern = @[@2,@3.75];
        _progressLayer.lineDashPattern = @[@2,@3.75];
//    }

    [progressPath addArcWithCenter:CGPointMake(self.width/2, self.height/2) radius:self.frame.size.height/3 startAngle:_startAngle/(180/M_PI) endAngle:(_startAngle+_progressAngle)/(180/M_PI) clockwise:YES];
    _progressLayer.path = progressPath.CGPath;
    
    //动画
    CABasicAnimation *pathAnimation = [CABasicAnimation animationWithKeyPath:@"strokeEnd"];
    pathAnimation.duration = [[NSString stringWithFormat:@"%0.2f",(float)_progressAngle/(float)_endAngle] floatValue];
    pathAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
    pathAnimation.fromValue = @(0);
    pathAnimation.toValue = @(1);
    pathAnimation.autoreverses = NO;   //回放
    [_progressLayer addAnimation:pathAnimation forKey:@"strokeEndAnimation"];
    _progressLayer.strokeEnd = 1;

}
- (void)setNeedsDisplay
{
    [super setNeedsDisplay];
}
//- (void)setStartAngle:(NSInteger)startAngle
//{
//    _startAngle = startAngle;
//    [self setNeedsDisplay];
//}
//- (void)setEndAngle:(NSInteger)endAngle
//{
//    _endAngle = endAngle;
//    [self setNeedsDisplay];
//}
//- (void)setProgressAngle:(NSInteger)progressAngle
//{
//    _progressAngle = progressAngle;
//    [self setNeedsDisplay];
//}
//- (void)setAutoreverses:(BOOL)autoreverses
//{
//    _autoreverses = autoreverses;
//    [self setNeedsDisplay];
//}
//- (void)setLineWidth:(CGFloat)lineWidth
//{
//    _lineWidth = lineWidth;
//    [self setNeedsDisplay];
//}
//- (void)setBottomColor:(UIColor *)bottomColor
//{
//    _bottomColor = bottomColor;
//    [self setNeedsDisplay];
//}
//- (void)setProgressColor:(UIColor *)progressColor
//{
//    _progressColor = progressColor;
//    [self setNeedsDisplay];
//}




@end
